package 天梯赛.tts44.bcj;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    static  int [] f;
    static  int maxLen= (int) (1e4+10);
    public static  int find(int x){
        return f[x]==x?x:(f[x]=find(f[x]));
    }
    public static  void  union(int x,int y){
        int u=find(x);
        int v=find(y);
        if (u!=v){
            f[u]=v;
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        f=new int[maxLen];
        for (int i = 1; i <=maxLen ; i++) {
            f[i]=i;
        }
        int temp[]=new int[maxLen];
        Set<Integer> set=new HashSet<>();

        while (n-->0){
            int k=sc.nextInt();
            for (int i = 0; i < k; i++) {
                temp[i]=sc.nextInt();
                set.add(temp[i]);
                if (i>0){
                    union(temp[i-1],temp[i]);
                }
            }
        }
        int num1=0;
        for(int i:set){
            if (i==find(i)){
                num1++;
            }
        }
        System.out.println(set.size()+" "+num1);

        int t=sc.nextInt();
        while (t-->0){
            int x=sc.nextInt();
            int y=sc.nextInt();
            if (find(x)==find(y)){
                System.out.println("Y");
            }else {
                System.out.println("N");
            }
        }
    }
}
